﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Practices.Prism.MefExtensions;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition.Hosting;
using ChrizNet.SearchService;
using ChrizNet.Infrastructure;
using ChrizNet.UI;
using ChrizNet.TranslateService;
using ChrizNet.MainMenuUI;


namespace ChrizNet.MainApp
{
    /// <summary>
    /// The MEF bootstrapper class.
    /// </summary>
    public class ChrizNetBootStrapper : MefBootstrapper
    {
        /// <summary>
        /// Configure the container.
        /// </summary>
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        /// <summary>
        /// Create a shell for bootstrapper.
        /// </summary>
        /// <returns>The main window shell.</returns>
        protected override System.Windows.DependencyObject CreateShell()
        {
            return this.Container.GetExportedValue<MainWindow>();
        }

        /// <summary>
        /// Initialize the shell.
        /// </summary>
        protected override void InitializeShell()
        {
            base.InitializeShell();


            //SplashScreen sc = new SplashScreen("Images/SplashImage.png");
            //sc.Show(true, true); //Set topmost to true to make sure splash screen will not be hidden 

            App.Current.MainWindow = this.Shell as MainWindow;
            App.Current.MainWindow.Show();
        }

        /// <summary>
        /// Configure the aggregate catalog by loading modules in the container.
        /// </summary>
        protected override void ConfigureAggregateCatalog()
        {
            base.ConfigureAggregateCatalog();

            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MainWindow).Assembly)); //MainUI assembly
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(RegionNames).Assembly)); //Infrastructure assembly
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(UIModule).Assembly)); //Storage module assembly
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(SearchServiceModule).Assembly)); //MainUI assembly
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(TranslateServiceModule).Assembly)); //MainUI assembly
            this.AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(MainMenu).Assembly)); //MainMenu assembly


            String directoryModules = "DirectoryModules";
            var path = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), directoryModules);
            if (Directory.Exists(path))
            {
                DirectoryCatalog catalog = new DirectoryCatalog(path);
                this.AggregateCatalog.Catalogs.Add(catalog);

                var directories = Directory.GetDirectories(path);

                foreach (var dir in directories)
                {
                    catalog = new DirectoryCatalog(dir);
                    this.AggregateCatalog.Catalogs.Add(catalog);
                }
            }
        }

        /// <summary>
        /// Create and add a region behaviour factory.
        /// </summary>
        /// <returns>The region behaviour factory.</returns>
        protected override IRegionBehaviorFactory ConfigureDefaultRegionBehaviors()
        {
            var factory = base.ConfigureDefaultRegionBehaviors();
            factory.AddIfMissing("AutoPopulateExportedViewsBehavior", typeof(AutoPopulateExportedViewsBehavior));

            return factory;
        }
    }
}
